package com.olinsports.system.olinsportssystem.service;

import com.olinsports.system.olinsportssystem.model.*;
import com.olinsports.system.olinsportssystem.repository.SavingDetailRepository;
import com.olinsports.system.olinsportssystem.util.PageUtil;
import com.olinsports.system.olinsportssystem.util.SavingDetailExcel;
import com.olinsports.system.olinsportssystem.util.ViewExcel;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/22.
 */
@Service
public class SavingDetailService {
    @Autowired
    private SavingDetailRepository savingDetailRepository;
   
    private int totalPages = 0;//总页数

    private int  totalRows;//总数据量
    @Autowired
    private PageUtil pageUtil;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private HttpServletResponse response;


    public SavingDetail selectBySaveKey(int saveKey){
        return savingDetailRepository.selectBySaveKey(saveKey);
    }

    public Map listSavingDetail(String page, String rows,String sord, String sidx){
        int beg = (Integer.valueOf(page)-1)*Integer.valueOf(rows);
            //查询当前表中的总数据量
            totalRows = savingDetailRepository.findCount();
            System.out.println("totalRows = "+totalRows);
            //通过分页进行查询当前表中的数据
            List<SavingDetail> stmCountList =  savingDetailRepository.findLimit(sord,sidx,beg,Integer.valueOf(rows));

            Map mapList = pageUtil.paging(rows,page,totalRows);
            mapList.put("rows",stmCountList);//设置行显示的数据

            return mapList;
    }

    public Map searchDate(String page, String rows,String startDate,String endDate){
        int beg = (Integer.valueOf(page)-1)*Integer.valueOf(rows);
        totalRows = savingDetailRepository.selectByDateCount(startDate,endDate);
        List<SavingDetail> stmCountList =  savingDetailRepository.selectByDateLimit(startDate,endDate,beg,Integer.valueOf(rows));
        Map mapList = pageUtil.paging(rows,page,totalRows);
        mapList.put("rows",stmCountList);//设置行显示的数据
        return mapList;
    }


    public ModelAndView toDcExcel(String startDate, String endDate, HSSFWorkbook workbook) throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        List<SavingDetail> savingDetails = savingDetailRepository.selectByDateAll(startDate,endDate);
        map.put("inventoryDetailss",savingDetails);
        SavingDetailExcel savingDetailExcel = new SavingDetailExcel("存款明细","存款明细");
        savingDetailExcel.buildExcelDocument(map, workbook, request, response);
        return new ModelAndView(savingDetailExcel, map);
    }

    public Map searchSelf(String page, String rows,String field,String fieldValue){
        int beg = (Integer.valueOf(page)-1)*Integer.valueOf(rows);
        totalRows = savingDetailRepository.selectSelfCount(field,fieldValue);
        List<SavingDetail> savingDetails =  savingDetailRepository.selectSelfLimit(field,fieldValue,beg,Integer.valueOf(rows));
        Map mapList = pageUtil.paging(rows,page,totalRows);
        mapList.put("rows",savingDetails);//设置行显示的数据
        return mapList;
    }

    public int insertAll(SavingDetail savingDetail){
        return savingDetailRepository.insertAll(savingDetail);
    }


    public int getSumTotal(){
        List<SavingDetail> savingDetails = savingDetailRepository.selectAll();
        int sumTotal = 0;
        for(SavingDetail savingDetail : savingDetails){
            sumTotal += savingDetail.getAmount();
        }
        return sumTotal;
    }

}
